package com.kingsoft.dc.khaos.module.spark.util

import java.text.SimpleDateFormat
import java.util.{Date, Locale}

import org.apache.commons.lang3.time.FastDateFormat

/**
  * 时间处理工具类
  *
  */
object DateUtils extends Serializable {

  //输入文件日期时间格式
  val ORIGIN_FORMAT = FastDateFormat.getInstance("dd/MMM/yyyy:HH:mm:ss Z", Locale.ENGLISH)

  //目标日期格式
  val TARGET_FORMAT = FastDateFormat.getInstance("yyMMddHHmm")

  val DATEFORMAT1 = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss")

  val DATEFORMAT2 = new SimpleDateFormat("yyyy-MM-dd")

  val DATETIMEFORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

  val DATETIMEFORMAT2 = new SimpleDateFormat("yyyyMMdd HH:mm:ss")

  val TIMEFORMAT = new SimpleDateFormat("HH:mm:ss")

  /**
    * 校验时间格式
    *
    * @param dateStr
    * @return
    */
  def isValidTime(dateStr: String): Boolean = {
    var convertSuccess = true
    try {
      TIMEFORMAT.setLenient(false)
      TIMEFORMAT.parse(dateStr)
    } catch {
      case _ => convertSuccess = false
    }
    convertSuccess
  }

  /**
    * 校验日期时间格式
    *
    * @param dateStr
    * @return
    */
  def isValidDateTime(dateStr: String): Boolean = {
    var convertSuccess = true
    try {
      DATETIMEFORMAT.setLenient(false)
      DATETIMEFORMAT.parse(dateStr)
    } catch {
      case _ => convertSuccess = false
    }
    convertSuccess
  }

  /**
    * 校验日期格式
    *
    * @param dateStr
    * @return
    */
  def isValidDate(dateStr: String): Boolean = {
    var convertSuccess = true
    try {
      DATEFORMAT2.setLenient(false)
      DATEFORMAT2.parse(dateStr)
    } catch {
      case _ => convertSuccess = false
    }
    convertSuccess
  }

  /**
    * 获取日志时间，long类型
    */
  def getTime(time: String) = {
    try {
      ORIGIN_FORMAT.parse(time.substring(time.indexOf('[') + 1, time.lastIndexOf(']'))).getTime
    } catch {
      case e: Exception =>
        println(s"parse time: $time error: " + e.getMessage)
        throw e
    }
  }

  /**
   * 时间格式转换
   *
   * @param dateStr      //日期字符串
   * @param sourceFormat //原日期格式
   * @param targetFormat //目标日期格式
   * @return
   */
  def dateFormatConvert(dateStr: String, sourceFormat: String, targetFormat: String): String = {
    val sformat = new SimpleDateFormat(sourceFormat)
    val tformat = new SimpleDateFormat(targetFormat)
    try {
      val date = sformat.parse(dateStr)
      tformat.format(date)
    } catch {
      case e: Exception =>
        println(s"date format convert error ！" + e.getMessage)
        throw e
    }
  }

  /**
    * 获取时区
    */
  def getTimezone(time: String) = {
    try {
      time.substring(time.indexOf('[') + 1, time.lastIndexOf(']')).split(" ")(1)
    } catch {
      case e: Exception =>
        println(s"parse timezone: $time error: " + time + e.getMessage)
        "-"
    }
  }

  /**
    * 处理时间
    *
    * @param time dd/MMM/yyyy:HH:mm:ss Z类型的字符串
    * @return 成功：yyyyMMddHHmm类型的字符串； 失败：197001010000
    */
  def parseToMinute(time: String) = {

    try {
      TARGET_FORMAT.format(new Date(getTime(time)))
    } catch {
      case e: Exception =>
        println(s"parse time: $time error: " + e.getMessage)
        throw e
    }
  }

  /**
    * 获取日期
    *
    * @param minute yyyyMMddHHmm类型的字符串
    * @return yyyyMMdd
    */
  def getDay(minute: String) = {
    minute.substring(0, 6)
  }

  /**
    * 获取小时
    *
    * @param minute yyyyMMddHHmm类型的字符串
    * @return HH
    */
  def getHour(minute: String) = {
    minute.substring(6, 8)
  }

  /**
    * 获取分钟，每5分钟一个间隔
    *
    * @param minute yyyyMMddHHmm类型的字符串
    * @return
    */
  def get5Min(minute: String) = {
    val m = minute.substring(8, 10).toInt
    if (m < 10)
      "0" + m / 5 * 5
    else
      "" + m / 5 * 5
  }

  /**
    * 获取分钟
    *
    * @param minute yyyyMMddHHmm类型的字符串
    * @return
    */
  def getMin(minute: String) = {
    minute.substring(8, 10)
  }

  /**
    * 获取当前时间
    *
    * @return
    */
  def getNowTime(): String = {
    val tm = System.currentTimeMillis()
    val tim = DATEFORMAT1.format(new Date(tm))
    tim
  }

  /**
   * 获取业务时间的时间戳(毫秒)
   * @param bizDate
   * @param bizTime
   * @return long
   */
  def getBizDateTimestamp(bizDate:String,bizTime:String): Long ={
    val date: Date = DATETIMEFORMAT2.parse(bizDate + " " + bizTime)
    date.getTime
  }


  def main(args: Array[String]): Unit = {
    val format = FastDateFormat.getInstance("yyMMddHHmm")
    val str = "1512459900000"
    val res = format.format(str.toLong)
    println(res)
  }
}
